//
//  PastBooking.m
//  bbqpitbooking
//
//  Created by Kyaw Phyu Aung on 6/6/11.
//  Copyright 2011 iss. All rights reserved.
//

#import "PastBooking.h"
#import "bbqpitbookingAppDelegate.h"
#import "BBQPitBooking.h"
#import "Customer.h"
#import "BBQPit.h";
#import "Park.h"
#import "ParkArea.h"

@implementation PastBooking

@synthesize pastBooking;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self FindPastBookings];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
	[self.pastBooking release];
}

//FindContact is to be used in ViewPastBooking Page
- (void) FindPastBookings
{
	bbqpitbookingAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	NSManagedObjectContext *context = [appDelegate managedObjectContext];
	NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Booking" inManagedObjectContext:context];
	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	[request setEntity:entityDesc];
	
	self.pastBooking = [[NSMutableArray alloc] init];
	BBQPitBooking *b;
	
	NSError *error;
	NSString *customerNRIC;
	NSString *parkName;
	NSString *parkAreaName;
	NSString *pitNumber;
	NSDate *bookingDate;
	
	NSArray *objects = [context executeFetchRequest:request error:&error];
	
	if ([objects count] > 0) {
		for(NSManagedObject *item in objects){
			customerNRIC = [item valueForKey:@"CustomerNRIC"];
			parkName = [item valueForKey:@"ParkName"];
			parkAreaName = [item valueForKey:@"ParkAreaName"];		
			pitNumber = [item valueForKey:@"PitNumber"];
			bookingDate = [item valueForKey:@"BookingDate"];
			
			b = [[BBQPitBooking alloc] init];
			b.customer.customerNRIC = customerNRIC;
			b.bbqPit.parkArea.park.parkName = parkName;
			b.bbqPit.parkArea.areaName = parkAreaName;
			b.bbqPit.assignedPitName = pitNumber;
			b.bookingDate = bookingDate;
			
			[self.pastBooking addObject:b];
			[b release];
		}
			
	}
	[request release];
}

//TableView Methods
//Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.pastBooking count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell
	BBQPitBooking *booking = [self.pastBooking objectAtIndex:indexPath.row];
	
	NSString *parkAndArea = [NSString stringWithFormat:@"%@/%@",booking.bbqPit.parkArea.park.parkName,booking.bbqPit.parkArea.areaName];
	
	cell.textLabel.text = parkAndArea;
	cell.detailTextLabel.text = booking.bbqPit.assignedPitName;
	
    cell.accessoryType = UITableViewCellAccessoryNone;
	//cell.font =[UIFont fontWithName:@"Helvetica" size:12.0];
    return cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}



@end
